<?php

namespace App\Models\Site;

use App\Models\BasicModel;
use Dcat\Admin\Traits\HasDateTimeFormatter;

class SiteConfig extends BasicModel
{
    use HasDateTimeFormatter;

    protected $table = 'site_config';
    public $timestamps = false;


    /**
     * @param $data
     */
    public function saveConfig($data)
    {
        foreach ($data as $key => $value) {
            $where['key'] = $key;
            $item['value'] = $value;
            self::updateOrInsert($where, $item);
        }
    }

    /**
     * @param $key
     * @return array|mixed
     */
    public function getConfig($key)
    {
        $where['key'] = $key;
        $value = self::where($where)->value('value');
        $result = json_decode($value);
        if (is_null($result)) $result[$key] = $value;
        return $result;
    }

}
